import type { RouteRecordRaw } from "vue-router";

export const SET_TOKEN = (token: string) => {
  localStorage.setItem('token', token)
}

export const GET_TOKEN = () => {
  return localStorage.getItem('token')
}

export const GET_USERNAME = () => {
  return localStorage.getItem('userName')
}

export const SET_USERNAME = (username: string) => {
  localStorage.setItem('userName', username)
}

export const GET_AVATAR = () => {
  return localStorage.getItem('avatar')
}

export const SET_AVATAR = (avatar: string) => {
  localStorage.setItem('avatar', avatar)
}
export const GET_PHONE = () => {
  return localStorage.getItem('phone')
}
export const SET_PHONE = (phone: string) => {
  localStorage.setItem('phone', phone)
}
export const GET_EMAIL = () => {
  return localStorage.getItem('email')
}
export const SET_EMAIL = (email: string) => {
  localStorage.setItem('email', email)
}
export const GET_PART = () => {
  return localStorage.getItem('part')
}
export const SET_PART = (part: string) => {
  localStorage.setItem('part', part)
}
export const GET_POSITION = () => {
  return localStorage.getItem('position')
}
export const SET_POSITION = (position: string) => {
  localStorage.setItem('position', position)
}
export const REMOVE_TOKEN = () => {
  localStorage.removeItem('token')
}
export const REMOVE_AVATAR = () => {
  localStorage.removeItem('avatar')
}
export const REMOVE_USERNAME = () => {
  localStorage.removeItem('userName')
}
export const REMOVE_PHONE = () => {
  localStorage.removeItem('phone')
}
export const REMOVE_EMAIL = () => {
  localStorage.removeItem('email')
}
export const REMOVE_PART = () => {
  localStorage.removeItem('part')
}
export const REMOVE_POSITION = () => {
  localStorage.removeItem('position')
}
export const SET_MENU = (menu: any) => {
  localStorage.setItem('menu', JSON.stringify(menu))
}
export const GET_MENU = () => {
  const menu = localStorage.getItem('menu')
  let menuList: RouteRecordRaw[] = [
    {
      path: '/',
      component: () => import('@/layout/index.vue'),
      name: 'layout',
      meta: {
        title: '',
        hidden: false,
        icon: ''
      },
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页',
            hidden: false,
            icon: 'House'
          }
        }
      ]
    }
  ]
  if (menu) {
    let data = JSON.parse(menu)
    let map: any = new Map()
    data.map((item: any) => {
      if (!map.has(item.SysID)) {
        let obj: {SysMenuID: string[] } = {SysMenuID: []}
        if (item.SysMenuID) {
          obj.SysMenuID.push(item.SysMenuID)
        }
        map.set(item.SysID, obj)
      } else {
        map.get(item.SysID).SysMenuID.push(item.SysMenuID)
      }
    })
    console.log(map)
    for (const [key, value] of map) {
      let item = {
        path: '/' + key,
        component: () => import('@/layout/index.vue'),
        name: key,
        meta: {
          title: key,
          icon: 'Aim',
          hidden: false,
        },
        redirect: '/OperationCenter/storage',
        children: []
      }
      for (let i = 0;  i < value.SysMenuID.length; i++) {
        let childItem = {
          path: '/OperationCenter/' + value.SysMenuID[i],
          component: () => import('@/views/OperationCenter/storage/index.vue'),
          name: value.SysMenuID[i],
          meta: {
            title: value.SysMenuID[i],
            hidden: false,
            icon: 'Connection'
          }
        }
        item.children.push(childItem)
      }
      menuList.push(item) 
    }
    return menuList
  }
  return null
}
export const REMOVE_MENU = () => {
  localStorage.removeItem('menu')
}
